<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/3
 * Time: 16:56
 */

namespace app\admin\controller;

//发货单单
use app\Model\AdminModel;
use app\Model\DeliveryDocModel;
use app\Model\GoodsModel;
use app\Model\OrderGoodsModel;
use app\Model\OrderModel;
use app\Model\ReturnGoods;
use app\Model\SpecGoodsPriceModel;

class Orderdelivery extends Common
{
    /*
     * 发货单列表
     */
    public function index()
    {
        $postData = input('get.');
//        $where['shipping_status']=['<>',1];//发货状态默认非发货状态
        $where['order_status']=['in','1,2,4'];//订单状态

        if (isset($postData['consignee'])){
            if ($postData['consignee']!=''){//收货人
                $where['consignee'] = ['LIKE', '%'.$postData['consignee'].'%'];
            }
            if ($postData['order_sn']!=''){//订单编号
                $where['order_sn'] = ['LIKE', '%'.$postData['order_sn'].'%'];
            }
            if ($postData['shipping_status']!='-1'){//发货状态
                $where['shipping_status'] = ['=', $postData['shipping_status']];
            }
        }
        $getReturnGoods = OrderModel::
            where($where)
            ->paginate(null,false,['query'=>request()->param()]);
        $data['data']=$getReturnGoods;
        $this->getConAndAct('order');
        return view('index',$data);
    }
    /*
     * 去发货（发货单配置）页面
     */
    public function add()
    {
        $postData = input('get.');
        $data['order'] = OrderModel::find($postData['order_id']);//订单信息
        if ($data['order']->order_status!=1&&$data['order']->order_status!=4){$this->error('只有已确定的订单才能发货');}
        $data['goods']=OrderGoodsModel::where('order_id','=',$postData['order_id'])->select();  //已有商品
        $data['deliverylog']=DeliveryDocModel::where('order_id','=',$postData['order_id'])->order('id','desc')->select();//发货记录
        $data['admin']=AdminModel::column('user_name','admin_id');//管理员
        return view('add', $data);
    }
    
    /*
     * 发货
     */
    public function addData()
    {
        //添加发货记录
        $postData=input('post.');
        $order = OrderModel::find($postData['order_id']);//获取订单信息
        if ($order->order_status!=1){return retJson('',201,'只有已确定的订单才能发货');}
        $insert =[
            'order_id'=>$order->order_id,
            'order_sn'=>$order->order_sn,//订单编号
            'user_id'=>$order->user_id,
            'admin_id'=>session('admin')->admin_id,
            'consignee'=>$order->consignee,
            'zipcode'=>$order->zipcode,//邮编
            'mobile'=>$order->mobile,//手机号码

            'address'=>$order->address,//详细地址
            'shipping_code'=>$order->shipping_code,//物流code
            'shipping_name'=>$order->shipping_name,//物流名
            'shipping_price'=>$order->shipping_price,//运费
            'invoice_no'=>$postData['invoice_no'],//物流单号
            'note'=>$postData['note'],//备注
            'create_time'=>time(),//创建时间
        ];
        $this->setStoreCount($postData['order_id']);//减库存
        $addDeliveryDoc = DeliveryDocModel::insertGetId($insert);
        setDatabaseDef($addDeliveryDoc,'发货记录添加失败');
        $updata=['is_send'=>1,'delivery_id'=>$addDeliveryDoc, ];//发货状态，发货id
        $orderGoods = OrderGoodsModel::where('order_id','=',$order->order_id)->update($updata); //改变订单商品的发货状态
        setDatabaseDef($orderGoods,'订单商品发货状态改变失败');
        //改变订单表的发货状态
        $this->setOrderC($order,'shipping_status',1,'确定发货',$postData['note']);
        //库存减少

        return retJson('',200,'发货成功');

    }
    /*
     * 减少相应库存（含规格）
     * $orderGoodsId 订单链接商品表id
     */
    public function setStoreCount($orderGoodsId)
    {
        $getOrderGoods=OrderGoodsModel::where('order_id','=',$orderGoodsId)->select();
        foreach ($getOrderGoods as $k=>$v){
            if ($v->spec_key==''){//没有规格
                GoodsModel::where('goods_id','=',$v->goods_id)->setDec('store_count',$v->goods_num);

            }else{//有规格
                SpecGoodsPriceModel::where('goods_id','=',$v->goods_id)
                    ->where('key','=',$v->spec_key)->setDec('store_count',$v->goods_num);
            }
        }
    }
}